/*
 * SdlEventManager.h
 *
 *  Created on: 2016年5月17日
 *      Author: terry
 */

#ifndef SDLEVENTMANAGER_H_
#define SDLEVENTMANAGER_H_

#include "SdlEventHandler.h"
#include "TThread.h"
#include "TCriticalSection.h"
#include <map>
#include "SharedPtr.h"


namespace sdl
{

class EventManager;

typedef std::shared_ptr< EventManager >	EventManagerPtr;


class EventManager : protected comn::Thread
{
public:
	static EventManagerPtr create();
	static void destroy(EventManagerPtr& manager);

	enum Constant
	{
		EVENT_MANAGER = SDL_LASTEVENT - 2,
	};

public:
	EventManager();
	virtual ~EventManager();

	int open();

	void close();

	bool isOpen();

    bool add(Uint32 winID, EventHandler* pHandler);

    void remove(Uint32 winID);

protected:
    virtual int run();
    virtual bool startup();
    virtual void cleanup();
    virtual void doStop();

protected:
    void handleEvent(SDL_Event& event);
    void broadcastEvent(SDL_Event& event);
    bool handleWindowEvent(Uint32 winID, SDL_Event& event);

    void handleManagerEvent(SDL_Event& sdlEvent);

protected:
    typedef std::map< Uint32, EventHandler* >  EventHandlerMap;

protected:
    comn::CriticalSection   m_cs;
    EventHandlerMap m_handlerMap;


};


} /* namespace sdl */

#endif /* SDLEVENTMANAGER_H_ */
